#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int maxn=1000000, inf=4*maxn;

class SegmentTree {
	public:
		int v[4*maxn+1], mv[4*maxn+1];

		void set(int o, int l, int r, int t, int tv) {
			if (l==r) v[o] = mv[o] = tv;
			else {
				int mid=(l+r)/2;
				if (t<=mid) set(o*2, l, mid, t, tv);
				else set(o*2+1, mid+1, r, t, tv);
				v[o] = max(v[o*2], v[o*2+1]);
				mv[o] = min(mv[o*2], mv[o*2+1]);
			}
		}

		int getMin(int o, int l, int r, int tl, int tr) {
			if (tl>tr) return 0;
			if (l==tl && r==tr) return mv[o];
			else {
				int mid=(l+r)/2, ret=v[o];
				if (tl<=mid) ret=min(ret, getMin(o*2, l, mid, tl, min(tr, mid)));
				if (tr>mid) ret=min(ret, getMin(o*2+1, mid+1, r, max(tl, mid+1), tr));
				return ret;
			}
		}

		int getLeft(int o, int l, int r, int tl, int tr, int tv) {
			if (tl>tr || v[o]<tv) return 0;
			int mid=(l+r)/2;
			if (l==tl && r==tr) {
				if (mv[o]>=tv) return l;
				else return v[o*2]>=tv ?
						getLeft(o*2, l, mid, l, mid, tv) :
						getLeft(o*2+1, mid+1, r, mid+1, r, tv);
			} else {
				int temp=0;
				if (tl<=mid) {
					temp = getLeft(o*2, l, mid, tl, min(tr, mid), tv);
				}
				if (temp) return temp;
				else return getLeft(o*2+1, mid+1, r, max(tl, mid+1), tr, tv);
			}
		}

		int getRight(int o, int l, int r, int tl, int tr, int tv) {
			if (tl>tr || v[o]<tv) return 0;
			int mid=(l+r)/2;
			if (l==tl && r==tr) {
				if (mv[o]>=tv) return r;
				else return v[o*2+1]>=tv ?
						getRight(o*2+1, mid+1, r, mid+1, r, tv) :
						getRight(o*2, l, mid, l, mid, tv);
			} else {
				int temp=0;
				if (tr>mid) {
					temp = getRight(o*2+1, mid+1, r, max(tl, mid+1), tr, tv);
				}
				if (temp) return temp;
				else return getRight(o*2, l, mid, tl, min(tr, mid), tv);
			}
		}
};

int main() {
	freopen("snow.in", "r", stdin);
	freopen("snow.out", "w", stdout);

	static SegmentTree sgt[2];
	static int row[maxn+1], col[maxn+1];
	int n, m, q;
	scanf("%d %d %d", &n, &m, &q);
	for (int i=1; i<=q; i++) {
		int opt, x;
		scanf("%d", &opt);
		if (opt==1) {
			scanf("%d", &x);
			sgt[0].set(1, 1, n, x, i);
			row[x] = i;
		} else if (opt==2) {
			scanf("%d", &x);
			sgt[1].set(1, 1, m, x, i);
			col[x] = i;
		} else {
			int x1, y1, x2, y2, k, a, b, c, d;
			scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
			a=row[x1], b=col[y1];
			c=row[x2], d=col[y2];
			if (x1>x2) swap(x1, x2);
			if (y1>y2) swap(y1, y2);

			int ans=inf;
			if ((a>=i-k && d>=i-k) || (b>=i-k && c>=i-k)) {
				ans = x2-x1+y2-y1;
			} else if (sgt[0].getMin(1, 1, n, x1, x2)>=i-k || sgt[1].getMin(1, 1, m, y1, y2)>=i-k) {
				ans = x2-x1+y2-y1;
			} else {
				if (a>=i-k && c>=i-k) {
					int u=sgt[1].getRight(1, 1, m, 1, y2, i-k),
						v=sgt[1].getLeft(1, 1, m, y1, m, i-k);
					ans = min(min(u ? abs(y1-u)+abs(y2-u) : inf, v ? abs(v-y1)+abs(v-y2) : inf)+x2-x1, ans);
				}
				if (b>=i-k && d>=i-k) {
					int u=sgt[0].getRight(1, 1, n, 1, x2, i-k),
						v=sgt[0].getLeft(1, 1, n, x1, n, i-k);
					ans = min(u ? abs(x1-u)+abs(x2-u) : inf, v ? abs(v-x1)+abs(v-x2) : inf)+y2-y1;
				}
			}
			printf("%d\n", ans>=inf ? -1 : ans);
		}
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}
